package priv.pront.code.lanqiao.competition.course.date0403;

import java.util.Scanner;

public class 入学考试 {
    static int T, M;
    static int[] time;
    static int[] value;
    static int[][] dp;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        T = scanner.nextInt();
        M = scanner.nextInt();
        time = new int[M];
        value = new int[M];
        dp = new int[M][T + 1];
        for (int i = 0; i < M; i++) {
            time[i] = scanner.nextInt();
            value[i] = scanner.nextInt();
        }
//        初始化
        for (int i = time[0]; i <= T; i++) {
            dp[0][i] = value[0];
        }

        for (int i = 1; i < M; i++) {
            for (int j = 1; j <= T; j++) {
                if (j < time[i]) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j - time[i]] + value[i], dp[i - 1][j]);
                }
            }
        }
        System.out.println(dp[M - 1][T]);
    }
}
